<?php
class My_EventScheduleController extends MM_Controller_My_EventDetailAction
{	
	
	/**
	 * @var MM_Service_Admin_EventSchedule 会议议程
	 */
	private $serviceES = null;
	
	/**
	 * 初始化
	 */
	public function init()
	{
		parent::init();
		$this->serviceES = new MM_Service_Admin_EventSchedule();
	}
	
	/**
	 * index
	 */
	public function indexAction()
	{
		$this->helper->setPageView($this->pageview, WEB_TITLE, "eventschedule", null);
	}
	
	/**
	 * page
	 */
	public function pageAction()
	{
		if ( !empty($this->httpdata['act']) && isset($this->httpdata['act']) ) {
			$where = "eid = $this->eid and pesid = 0";
			$order = "num asc";
			$result = $this->serviceES->fetchAll(null,null,$where,$order);
			if ($result->getCode()==$result::CODE_FAILURE) echo $this->encodeResult($result);
			$data = $result->getData();
			$dt = new Mediad2_Utility_Datatable($data);
			$dt->init($this->httpdata['act'], array("name"));
			$dt->create();
			$dt->response();
		}
	}
	
	/**
	 * list event content
	 */
	public function listAction()
	{
    	if ( ($this->uid<1) || ($this->eid<1) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/eid');
    	} else {
    		$pesid = empty($this->httpdata['pesid'])?0:$this->httpdata['pesid'];
    		$where = "eid = $this->eid and pesid = $pesid";
    		$order = "num asc";
    		$this->result = $this->serviceES->fetchAll(null,null,$where,$order,true);
    	}
    	echo $this->encodeResult($this->result);
	}
	
	/**
	 * create event content
	 */
	public function createAction()
	{
    	if ( ($this->uid<1) || ($this->eid<1)
    			|| !isset($this->httpdata['pesid'])
    			|| !isset($this->httpdata['title']) || empty($this->httpdata['title']) 
    			|| !isset($this->httpdata['begindate']) || empty($this->httpdata['begindate'])
    			|| !isset($this->httpdata['startdate']) || empty($this->httpdata['startdate']) 
    			|| !isset($this->httpdata['enddate']) || empty($this->httpdata['enddate'])) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/title...');
    	} else {
    		//封装model
    		$model = new MM_Model_EventSchedule();
    		$model->setPesid(empty($this->httpdata['pesid'])?0:$this->httpdata['pesid']);
    		$model->setEid($this->eid);
    		$model->setTitle($this->httpdata['title']);    		
    		$model->setBegindate($this->httpdata['begindate']);
    		$model->setStartdate($this->httpdata['startdate']);
    		$model->setEnddate($this->httpdata['enddate']);
    		$num = $this->httpdata['num'];//by vincent num有默认值:1
    		if(!isset($num))$num = 1;
    		$model->setNum($num);
    		$model->setUid($this->uid);
    		$model->setAddtime(time());
    		
    		if (isset($this->httpdata['subtitle'])) $model->setSubtitle($this->httpdata['subtitle']);
    		if (isset($this->httpdata['desc'])) $model->setDesc($this->httpdata['desc']);
    		$gids = @$_REQUEST['gids'];
//     		$this->helper->log("gids:".@$_REQUEST['gids']);
//     		if (isset($this->httpdata['gids'])) $model->setGids($this->httpdata['gids']);
    		if (isset($this->httpdata['gids'])) $model->setGids(implode(",", $gids));
    		if (isset($this->httpdata['venueid'])) $model->setVenueid($this->httpdata['venueid']);
    		if (isset($this->httpdata['venuedesc'])) $model->setVenuedesc($this->httpdata['venuedesc']);
    	
    		$this->result = $this->serviceES->insert($model);
    		//TODO:将嘉宾保存到活动嘉宾表
    		if($this->result->getCode() && isset($gids)){
    			$esid = $this->result->getData();
    			$eguest = new MM_Service_Admin_EventGuests();
    			foreach ($gids as $key => $value) {
    				$eguest->chooseGuests($this->uid, $value, $this->eid,$esid);
    			}
    		}
    		
    	}
    	echo $this->encodeResult($this->result);
	}
	
	/**
	 * update event content
	 */
	public function updateAction()
	{
    	if ( ($this->uid<1)
    			|| !isset($this->httpdata['esid']) || empty($this->httpdata['esid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/esid');
    	} else {
    		$esid = $this->httpdata['esid'];
    		$this->result = $this->serviceES->updateSchedule($esid, $this->httpdata);
    	}
    	echo $this->encodeResult($this->result);
	}
	
	/**
	 * delete event content
	 */
	public function delAction()
	{
    	if ( ($this->uid<1)
    			|| !isset($this->httpdata['esid']) || empty($this->httpdata['esid']) ) {
    		$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/esid');
    	} else {
    		$esid = $this->httpdata['esid'];
    		$this->result = $this->serviceES->delete($esid);
    	}
    	echo $this->encodeResult($this->result);
	}
	
	/**
	 * get event schedule
	 */
	public function getAction()
	{
		if ( ($this->uid<1)
				|| !isset($this->httpdata['esid']) || empty($this->httpdata['esid']) ) {
			$this->result = $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,'Miss parameter: uid/esid');
		} else {
			$esid = $this->httpdata['esid'];
			$this->result = $this->serviceES->fetchAll(null,null,"esid=$esid",null,true);
		}
		echo $this->encodeResult($this->result);
	}
}